package com.nanhai.buddhist.college.module.collegebase.dal.mysql.teacherteachers;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.mybatis.core.mapper.BaseMapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;

import com.nanhai.buddhist.college.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.teacherteachers.vo.TeacherTeachersExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.teacherteachers.vo.TeacherTeachersPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.colleges.CollegesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionsDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.teacherteachers.TeacherCountDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.teacherteachers.TeacherTeachersDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 教师管理 Mapper
 *
 * @author AKai
 */
@Mapper
public interface TeacherTeachersMapper extends BaseMapperX<TeacherTeachersDO> {

    default PageResult<TeacherTeachersDO> selectPage(TeacherTeachersPageReqVO reqVO) {
        return selectPageForJoin(reqVO,TeacherTeachersDO.class, new MPJLambdaWrapperX<TeacherTeachersDO>()
                .eqIfPresent(TeacherTeachersDO::getJobNumber, reqVO.getJobNumber())
                .likeIfPresent(TeacherTeachersDO::getLegalName, reqVO.getLegalName())
                .likeIfPresent(TeacherTeachersDO::getTeacherName, reqVO.getTeacherName())
                .eqIfPresent(TeacherTeachersDO::getTeacherSex, reqVO.getTeacherSex())
                .eqIfPresent(TeacherTeachersDO::getCollegeId, reqVO.getCollegeId())
                .leftJoin(CollegesDO.class,CollegesDO::getId,TeacherTeachersDO::getCollegeId)
                .selectAll(TeacherTeachersDO.class)
                .selectAs(CollegesDO::getCollegeName,TeacherTeachersDO::getCollegeName)
                .orderByDesc(TeacherTeachersDO::getCreateTime));
    }

    default List<TeacherTeachersDO> selectList(TeacherTeachersExportReqVO reqVO) {
        return selectJoinList(TeacherTeachersDO.class,new MPJLambdaWrapperX<TeacherTeachersDO>()
                .eqIfPresent(TeacherTeachersDO::getJobNumber, reqVO.getJobNumber())
                .likeIfPresent(TeacherTeachersDO::getLegalName, reqVO.getLegalName())
                .likeIfPresent(TeacherTeachersDO::getTeacherName, reqVO.getTeacherName())
                .eqIfPresent(TeacherTeachersDO::getTeacherSex, reqVO.getTeacherSex())
                .eqIfPresent(TeacherTeachersDO::getCollegeId, reqVO.getCollegeId())
                .leftJoin(CollegesDO.class, CollegesDO::getId, TeacherTeachersDO::getCollegeId)
                .orderByDesc(TeacherTeachersDO::getCreateTime));
    }

    default TeacherTeachersDO getUserByUsername(String username){
        return selectOne(new LambdaQueryWrapper<TeacherTeachersDO>().eq(TeacherTeachersDO::getJobNumber, username));
    }


    default List<TeacherCountDO> queryTeacherCountByCollegeIds(Collection<String> ids){
        MPJLambdaWrapper<TeacherTeachersDO> in = new MPJLambdaWrapperX<TeacherTeachersDO>()
                .selectCount(TeacherTeachersDO::getId, TeacherCountDO::getTeacherCount)
                .selectAs(TeacherTeachersDO::getCollegeId,TeacherCountDO::getCollegeId).select()
                .groupBy(TeacherTeachersDO::getCollegeId).in(TeacherTeachersDO::getCollegeId, ids);

        List<Map<String, Object>> maps1 = selectMaps(in);

        return BeanUtil.copyToList(maps1, TeacherCountDO.class);
    }

    default List<TeacherTeachersDO> selectBatchIdsJoin(Collection<String> ids){
        MPJLambdaWrapper<TeacherTeachersDO> in = new MPJLambdaWrapperX<TeacherTeachersDO>()
                .selectAs(TeacherTeachersDO::getCollegeId,TeacherCountDO::getCollegeId).select()
                .in(TeacherTeachersDO::getId, ids)
                .leftJoin(CollegesDO.class,CollegesDO::getId,TeacherTeachersDO::getCollegeId)
                .selectAll(TeacherTeachersDO.class)
                .selectAs(CollegesDO::getCollegeName,TeacherTeachersDO::getCollegeName);
        List<Map<String, Object>> maps1 = selectMaps(in);
        return BeanUtil.copyToList(maps1, TeacherTeachersDO.class);
    }




}
